package cn.sjx.auth.service.impl;

import cn.sjx.auth.domain.Role;
import cn.sjx.auth.mapper.RoleMapper;
import cn.sjx.auth.query.RoleQuery;
import cn.sjx.auth.service.RoleService;
import cn.sjx.basic.service.impl.BasicServiceImpl;
import cn.sjx.basic.utils.BasicQuery;
import cn.sjx.basic.utils.PageList;
import cn.sjx.dto.RolePermissionDTO;
import cn.sjx.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.management.Query;
import java.io.Serializable;
import java.util.List;

@Service
public class RoleServiceImpl extends BasicServiceImpl<Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;

//    @Override
//    public Role getById(Serializable id) {
//
//        return roleMapper.getById(id);
//    }
//
//    @Override
//    public List<Role> getAll() {
//        return roleMapper.getAll();
//    }
//
//    @Override
//    public void insert(Role role) {
//        roleMapper.insert(role);
//
//    }
//
//    @Override
//    public void update(Role role) {
//        roleMapper.update(role);
//    }
//
//    @Override
//    public void deleteById(Serializable id) {
//        roleMapper.deleteById(id);
//    }
//
//    @Override
//    public void batchRemove(List<Long> ids) {
//        if (ids == null || ids.size() == 0) {
//            throw new RuntimeException("删除错误");
//        }
//        roleMapper.batchRemove(ids);
//    }

    @Override
    public PageList<Role> pageList(BasicQuery basicQuery) {
        Long total = roleMapper.loadTotal(basicQuery);
        if (total > 0) {
            List<Role> list = roleMapper.loadData(basicQuery);
            return new PageList<>(total, list);

        }

        return new PageList<>();

    }

    @Transactional
    @Override
    public void setRolePermission(RolePermissionDTO dto) {
        //先判断dto中的数组是否有值，如果没有，直接删除
        List<String> permissionSns = dto.getPermissionSns();
        if (permissionSns.size() == 0) {
            roleMapper.delteRolePermissionByRoleId(dto.getRoleId());
            return;
        }
        //如果已经有权限了，则要先删除，再添加
        //无所谓，直接删，不影响
        roleMapper.delteRolePermissionByRoleId(dto.getRoleId());

        roleMapper.saveRolePermission(dto.getRoleId(), dto.getPermissionSns());
    }

    @Override
    public List<String> getPermissionByRoleId(Long roleId) {


        return roleMapper.getPermissionByRoleId(roleId);
    }
}
